package edu.hawaii.ics466.tasks;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import edu.hawaii.ics466.objects.GameInfo;
import edu.hawaii.ics466.objects.Review;

import android.os.AsyncTask;
import android.util.Log;

public class GetReviews extends AsyncTask<String, Void, Review[]> {

	@Override
	protected Review[] doInBackground(String... params) {
		String gameName = (params.length > 0) ? params[0] : "";

		Map<String, String> requestParams = new HashMap<String, String>();
		requestParams.put("gameName", gameName);
		String response = Ajax.send("http://the-game-rater.comuf.com/app/get_reviews.php", requestParams);
		if (response != null) {
			List<Review> reviews = new ArrayList<Review>();
			String[] rs = response.split(";");
			for (int i = 0; i < rs.length; i++) {
				String[] r = rs[i].split("`");
				if (r.length >= 4) {
					reviews.add(new Review(r[0], r[1], r[2], r[3]));
				}
			}
			Review[] temp = new Review[reviews.size()];
			return reviews.toArray(temp);
		}
		return null;
	}
}
